package com.ransj.iphone.view;

import com.ransj.iphone.IKey;
import com.ransj.iphone.IKeyListener;
import com.ransj.iphone.R;
import com.ransj.iphone.R.drawable;
import com.ransj.iphone.R.id;
import com.ransj.iphone.R.layout;
import com.ransj.iphone.R.string;
import com.ransj.iphone.utils.ILog;
import com.ransj.iphone.utils.IUtils;

import android.content.Context;
import android.media.AudioManager;
import android.os.AsyncTask;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager.LayoutParams;
import android.widget.CheckBox;

/**
 * 
 * @author ranshengju
 * 
 */
public class DevicePad extends AbsView implements OnClickListener {
	private static final String TAG = DevicePad.class.getSimpleName();
	private Context mContext;
	private View m_vDevice;
	private CheckBox m_cbGps;
	private CheckBox m_cbWifi;
	private CheckBox m_cbBluetooth;
	private CheckBox m_cbVolumeDown;
	private CheckBox m_cbVolumeUp;
	private CheckBox m_cbRinger;
	private CheckBox m_cbOrientation;
	private CheckBox m_cbMobileData;
	private CheckBox m_cbBack;
	private boolean m_isGpsOn;
	private boolean m_isWifiOn;
	private boolean m_isBlueToothOn;
	private boolean m_isAutoOrientationOn;
	private boolean m_isMobileDataOn;

	public DevicePad(Context context, LayoutParams params, IKeyListener listener) {
		mContext = context;
		mKeyListener = listener;
		mWMParams = params;
		init();
		ensureUI();
	}

	private void init() {
		mMainView = LayoutInflater.from(mContext).inflate(
				R.layout.iphone_device_pad, null);
		m_vDevice = mMainView.findViewById(R.id.device_pad);
		m_vDevice.setOnClickListener(this);
		m_cbGps = (CheckBox) mMainView.findViewWithTag(mContext
				.getString(R.string.device_tag_gps));
		m_cbGps.setOnClickListener(this);
		m_cbWifi = (CheckBox) mMainView.findViewWithTag(mContext
				.getString(R.string.device_tag_wifi));
		m_cbWifi.setOnClickListener(this);
		m_cbBluetooth = (CheckBox) mMainView.findViewWithTag(mContext
				.getString(R.string.device_tag_bluetooth));
		m_cbBluetooth.setOnClickListener(this);
		m_cbVolumeDown = (CheckBox) mMainView.findViewWithTag(mContext
				.getString(R.string.device_tag_volume_down));
		m_cbVolumeDown.setOnClickListener(this);
		m_cbVolumeUp = (CheckBox) mMainView.findViewWithTag(mContext
				.getString(R.string.device_tag_volume_up));
		m_cbVolumeUp.setOnClickListener(this);
		m_cbRinger = (CheckBox) mMainView.findViewWithTag(mContext
				.getString(R.string.device_tag_ring));
		m_cbRinger.setOnClickListener(this);
		m_cbOrientation = (CheckBox) mMainView.findViewWithTag(mContext
				.getString(R.string.device_tag_auto_orientation));
		m_cbOrientation.setOnClickListener(this);
		m_cbMobileData = (CheckBox) mMainView.findViewWithTag(mContext
				.getString(R.string.device_tag_mobile_data));
		m_cbMobileData.setOnClickListener(this);
		m_cbBack = (CheckBox) mMainView.findViewWithTag(mContext
				.getString(R.string.device_tag_back));
		m_cbBack.setOnClickListener(this);
		updateStates();
	}
	
	public void updateStates(){
		// init state
		m_isGpsOn = IUtils.isGpsOn(mContext);
		m_isWifiOn = IUtils.isWifiOn(mContext);
		m_isBlueToothOn = IUtils.isBluetoothOn();
		m_isAutoOrientationOn = IUtils.isAutoOrientation(mContext);
		m_isMobileDataOn = IUtils.isMobileDataOn(mContext);
		ensureUI();
	}

	@Override
	public void onClick(View v) {
		if (v.equals(m_vDevice)) {
			mKeyListener.onKeyEvent(this, IKey.KEY_PAD_OUTSIDE);
		} else if (v.equals(m_cbBack)) {
			mKeyListener.onKeyEvent(this, IKey.KEY_BACK_MAIN_PAD);
		} else {
			new SettingTask(this, v).execute();
		}
		ensureUI();
	}

	private void ensureUI() {
		m_cbGps.setChecked(m_isGpsOn);
		m_cbWifi.setChecked(m_isWifiOn);
		m_cbBluetooth.setChecked(m_isBlueToothOn);
		switch (IUtils.getRingerType(mContext)) {
		case AudioManager.RINGER_MODE_NORMAL:
			m_cbRinger
					.setBackgroundResource(R.drawable.selector_bg_device_ring);
			break;
		case AudioManager.RINGER_MODE_SILENT:
			m_cbRinger
					.setBackgroundResource(R.drawable.selector_bg_device_silent);
			break;
		case AudioManager.RINGER_MODE_VIBRATE:
			m_cbRinger
					.setBackgroundResource(R.drawable.selector_bg_device_vibrate);
			break;
		}
		m_cbOrientation.setChecked(m_isAutoOrientationOn);
		m_cbMobileData.setChecked(m_isMobileDataOn);
	}

	private static class SettingTask extends AsyncTask<Void, Void, Boolean> {
		private DevicePad mPad;
		private View mView;

		public SettingTask(DevicePad pad, View view) {
			mPad = pad;
			mView = view;
		}

		@Override
		protected Boolean doInBackground(Void... params) {
			try {
				if (mView.equals(mPad.m_cbGps)) {
					return IUtils.startGpsSetting(mPad.mContext);
				} else if (mView.equals(mPad.m_cbWifi)) {
					return IUtils.setWifiEnable(mPad.mContext,
							!mPad.m_isWifiOn);
				} else if (mView.equals(mPad.m_cbBluetooth)) {
					return IUtils.setBluetoothEnable(!mPad.m_isBlueToothOn);
				} else if (mView.equals(mPad.m_cbOrientation)) {
					return IUtils.setAutoOrientation(mPad.mContext,
							!mPad.m_isAutoOrientationOn);
				} else if (mView.equals(mPad.m_cbMobileData)) {
					return IUtils.setMobileDataEnable(mPad.mContext,
							!mPad.m_isMobileDataOn);
				} else if (mView.equals(mPad.m_cbRinger)) {
					if (IUtils.getRingerType(mPad.mContext) == AudioManager.RINGER_MODE_NORMAL) {
						return IUtils.setRingerType(mPad.mContext,
								AudioManager.RINGER_MODE_VIBRATE);
					} else if (IUtils.getRingerType(mPad.mContext) == AudioManager.RINGER_MODE_VIBRATE) {
						return IUtils.setRingerType(mPad.mContext,
								AudioManager.RINGER_MODE_SILENT);
					} else if (IUtils.getRingerType(mPad.mContext) == AudioManager.RINGER_MODE_SILENT) {
						return IUtils.setRingerType(mPad.mContext,
								AudioManager.RINGER_MODE_NORMAL);
					}
				} else if (mView.equals(mPad.m_cbVolumeUp)) {
					return IUtils.volumeUp(mPad.mContext);
				} else if (mView.equals(mPad.m_cbVolumeDown)) {
					return IUtils.volumeDown(mPad.mContext);
				}
			} catch (Exception e) {
				ILog.getInstance().e(TAG, e);
			}
			return false;
		}

		@Override
		protected void onPostExecute(Boolean result) {
			super.onPostExecute(result);
			if(result && mPad != null){
				if (mView.equals(mPad.m_cbGps)) {
					mPad.m_isGpsOn = !mPad.m_isGpsOn;
				} else if (mView.equals(mPad.m_cbWifi)) {
					mPad.m_isWifiOn = !mPad.m_isWifiOn;
				} else if (mView.equals(mPad.m_cbBluetooth)) {
					mPad.m_isBlueToothOn = !mPad.m_isBlueToothOn;
				} else if (mView.equals(mPad.m_cbOrientation)) {
					mPad.m_isAutoOrientationOn = !mPad.m_isAutoOrientationOn;
				} else if (mView.equals(mPad.m_cbMobileData)) {
					mPad.m_isMobileDataOn = !mPad.m_isMobileDataOn;
				}
				mPad.ensureUI();
			}
			mPad = null;
			mView = null;
		}

	}

}
